package com.xixiake.net;

import android.content.Context;

import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HttpStack;
import com.android.volley.toolbox.Volley;

/**
 * volley请求队列，用于并发处理HTTP请求
 * Created by c-wanghengli on 2017/4/16.
 */
public class NetClient {

    private static final String TAG = "NetClient";

    private static RequestQueue requestQueue;

    private static HttpStack stack;

    private static Context mContext;

    public static void init(Context context){

        if(requestQueue != null){
            requestQueue.stop();
        }
        requestQueue = newRequestQueue(context);

    }


    /**
     * 只在初始化的时候执行一次,根据Volley newRequestQueue源码修改。可以参考源码对比着来学
     *
     * @param context 上下文
     * @return 返回Volley的RequestQueue
     */
    private static RequestQueue newRequestQueue(Context context) {

       /* Network network = new BasicNetwork(stack);

        // 设置缓存目录，缓存的大小默认是5MB
        final DiskBasedCache diskCache = new DiskBasedCache(cacheDir);
        RequestQueue queue = new RequestQueue(diskCache, network);*/
        RequestQueue queue = Volley.newRequestQueue(context);
        queue.start();
        return queue;
    }

    public static void request(Request request){
        if(null != requestQueue && null != request ){
            requestQueue.add(request);
        }
    }

}
